/*
 * Copyright (c) 2009 Sven Bachmann (dev@mcbachmann.de)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <mysql/mysql.h> 
#include "ws.h"
#include <string.h>



/* variables */
struct config_s config;
struct record_s record;
struct version_s version;

/* prototypes */
void ws_print_human(void);
void ws_print_human_sensor(int, int, int);
void ws_print_json(void);
void ws_print_json_sensor(int, int, int, int);
void ws_print_raw(void);


MYSQL *sql_conn;	

  int len, size;
  char data[1000*1024];
  char chunk[2*1000*1024+1];
  char query[1024*5000];


/*
 * main
 *
 * Demonstration of weatherstation polling.
 */
int main(int argc, char **argv)
{
    int ret;

    /* initialize USB connection */
    check(ws_device_open(), goto bye);

    /* read stations version */
    check(ws_send_repeat(ws_version, &version, "ws_version(version)"), goto bye_close);

    /* read configuration */
    check(ws_send_repeat(ws_config_read, &config, "ws_config_read(config)"), goto bye_close);

    /* read current record */
    check(ws_send_repeat(ws_record_current, &record, "ws_record_current(config)"), goto bye_close);
	
    /* present the data */
    ws_print_json();

    /* all went fine */
    ret = 0;

bye_close:
    ws_device_close();

bye:
    return ret;
}

/*
 * ws_print_human
 *
 * Print data human readable.
 */
void ws_print_human(void)
{
    int cnt;
    printf("Version:                  %s\n",   VERSION);
    printf("Timestamp:                %lu\n",  ws_timestamp());
    printf("Version Station:          %i\n",   version.nr);
    printf("Wind Speed:               %.1f km/h\n", ws_conv_wind_speed(&record));
    printf("Rain Sensor:              %.1f mm\n", ws_conv_rainfall(&config, &record));
    printf("Pressure:                 %.0f hPa\n", ws_conv_pressure(&config, &record));
    printf("Forecast:                 %s\n",   ws_conv_forecast(&record));

    ws_print_human_sensor(0, record.station.temp, record.station.humidity);

    for (cnt = 0; cnt < 9; cnt++) {
	/* check if sensor is available */
	if ((config.sensor[cnt] & 0xF0) != WS_SENSOR_AVAIL) {
	    continue;
	}

	ws_print_human_sensor(cnt + 1, record.sensor[cnt].temp, record.sensor[cnt].humidity);
    }
}



/*
 * ws_print_human_sensor
 *
 * Print sensor specific data.
 */
void ws_print_human_sensor(int id, int temp, int hum)
{
    printf("Sensor %i\n", id);
    printf("  Temperature:            %.1f °C\n", ws_conv_temperature(temp));
    printf("  Humiditiy:              %i %%\n",   hum);
}

/*
 * ws_print_json
 *
 * Print data in json format.
 */
void ws_print_json(void)
{

char query[1024];
sql_conn = mysql_init (NULL);

	if(!mysql_real_connect (sql_conn, "localhost", "wetter","wetter123", "ws300", 3306, 0, 0))
	{
		printf ("%s\n", mysql_error (sql_conn));
	} 


	




    int cnt;
    int sensor_avail = 0;

	/* check if other sensors are available */
    for (cnt = 0; cnt < 9; cnt++) {
	if ((config.sensor[cnt] & 0xF0) != WS_SENSOR_AVAIL) {
	    continue;
	}
	sensor_avail++;
    }

    /* print local station data */
    //ws_print_json_sensor(0, record.station.temp, record.station.humidity, sensor_avail);
	
	/***** LOCAL DATA FOR MYSQL ******/
	char tempQuery[100];
	float temp = ws_conv_temperature(record.station.temp);
	int hum =  record.station.humidity;
	
    sprintf (query, "insert into Messwerte_1 (SensorID, Temperature, Humidity) VALUES "
			"(%i, %.1f, %i)", 0, temp, hum);

	
    /* show available sensors */
    for (cnt = 0; cnt < 9; cnt++) {

	/* check if sensor is available */
	if ((config.sensor[cnt] & 0xF0) != WS_SENSOR_AVAIL) {
	    continue;
	}
	float temp = ws_conv_temperature(record.sensor[cnt].temp);
	int hum =  record.sensor[cnt].humidity;
	
	sprintf(tempQuery, ",(%i, %.1f, %i)", cnt+1, temp, hum);
	strcat(query, tempQuery);
	//ws_print_json_sensor(cnt + 1, record.sensor[cnt].temp, record.sensor[cnt].humidity, --sensor_avail);
	
  
  	}
	strcat(query, ";");

	mysql_query (sql_conn, query);
	
	

	sprintf(query, "insert into Messwerte_2 (SensorID, Pressure, Rain, Forecast, WindSpeed) VALUES "
			"(%i, %.0f, %.1f, '%s', %.1f);", 0, ws_conv_pressure(&config, &record), ws_conv_rainfall(&config, &record), ws_conv_forecast(&record), ws_conv_wind_speed(&record));

	mysql_query (sql_conn, query);
	
	
	mysql_close (sql_conn);
	mysql_library_end ();
}

/*
 * ws_print_json_sensor
 *
 * Print sensor specific data.
 */
void ws_print_json_sensor(int id, int temp, int hum, int comma)
{


sprintf (query, "insert into Messwerte_1 (SensorID, Temperature, Humidity) VALUES (%i, %.1f, %i)", id, ws_conv_temperature(temp), hum);
	
	mysql_query (sql_conn, query);
		printf("Executing query: insert into Messwerte_1 (SensorID, Temperature, Humidity) VALUES (%i, %.1f, %i)", id, ws_conv_temperature(temp), hum);

    printf("    \"%i\":               {\n", id);
    printf("      \"temperature\":    \"%.1f\",\n", ws_conv_temperature(temp));
    printf("      \"humidity\":       \"%i\"\n", hum);
    printf("    }%s\n", (comma) ? "," : "");
}

/*
 * ws_print_raw
 *
 * Print raw data structures.
 */
void ws_print_raw(void)
{
    printf("Version: ");
    ws_dump(&version, sizeof(struct version_s));
    printf("\n");

    printf("Config:  ");
    ws_dump(&config, sizeof(struct config_s));
    printf("\n");

    printf("Record:  ");
    ws_dump(&record, sizeof(struct record_s));
    printf("\n");
}


